package com.nuc.bigdata.app.service.impl;

import com.nuc.bigdata.app.service.NewsService;
import com.nuc.bigdata.domain.entity.User;
import com.nuc.bigdata.domain.vo.CommentsVO;
import com.nuc.bigdata.domain.vo.NewsVO;
import com.nuc.bigdata.feign.NewsCommentsServiceFeign;
import com.nuc.bigdata.feign.NewsServiceFeign;
import com.nuc.bigdata.feign.PictureServiceFeign;
import com.nuc.bigdata.feign.UserServiceFeign;
import com.nuc.bigdata.feign.dto.NewsCommentsDto;
import com.nuc.bigdata.feign.entity.CommentsFeign;
import com.nuc.bigdata.feign.entity.NewsFeign;
import com.nuc.bigdata.feign.entity.PictureFeign;
import org.aspectj.weaver.NewFieldTypeMunger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/18
 */
@Service
public class NewsServiceImpl implements NewsService {
    private final NewsServiceFeign newsServiceFeign;

    private final PictureServiceFeign pictureServiceFeign;

    private final NewsCommentsServiceFeign newsCommentsServiceFeign;

    private final UserServiceFeign userServiceFeign;

    @Autowired
    NewsServiceImpl(NewsServiceFeign newsServiceFeign,
                    PictureServiceFeign pictureServiceFeign,
                    NewsCommentsServiceFeign newsCommentsServiceFeign,
                    UserServiceFeign userServiceFeign){
        this.pictureServiceFeign = pictureServiceFeign;
        this.newsServiceFeign = newsServiceFeign;
        this.newsCommentsServiceFeign = newsCommentsServiceFeign;
        this.userServiceFeign = userServiceFeign;
    }

    /**
     * 查询出所有的新闻VO
     * @return 包含新闻VO的ArrayList
     */
    @Override
    public ArrayList<NewsVO> listNews() {
        ArrayList<NewsFeign> newsFeigns = newsServiceFeign.listNews();
        ArrayList<NewsVO> newsVOArrayList = new ArrayList<>();
        for (NewsFeign newsFeign: newsFeigns){
            NewsVO newsVO = transformNewsFeign(newsFeign);
            newsVOArrayList.add(newsVO);
        }
        return newsVOArrayList;
    }

    /**
     * 根据指定新闻唯一标识获取新闻VO
     * @param newsId 新闻唯一标识
     * @return 一条新闻VO
     */
    @Override
    public NewsVO getNews(String newsId) {
        NewsFeign news = newsServiceFeign.getNews(newsId);
        NewsVO newsVO = transformNewsFeign(news);
        return newsVO;
    }

    /**
     * 根据指定新闻查询指定页码的评论VO集合
     * @param newsId 新闻唯一标识
     * @param pageSize 页码
     * @return 包含评论VO的ArrayList
     */
    @Override
    public ArrayList<CommentsVO> listNewsComments(String newsId, Long pageSize) {
        ArrayList<CommentsVO> commentsVOArrayList = new ArrayList<>();
        ArrayList<CommentsFeign> commentsFeigns = newsCommentsServiceFeign.queryCommentsFeign(newsId, pageSize);
        for(CommentsFeign commentsFeign: commentsFeigns){
            CommentsVO commentsVO = transformCommentFeign(commentsFeign);
            commentsVOArrayList.add(commentsVO);
        }
        return commentsVOArrayList;
    }

    /**
     * 提交新的评论
     * @param newsId 新闻唯一标识
     * @param commentsDto 评论内容Dto包含userId和content(内容)
     */
    @Override
    public void createNewsComment(String newsId,NewsCommentsDto commentsDto) {
        newsCommentsServiceFeign.createComments(newsId,commentsDto );
    }

    /**
     * 用户对评论点赞
     * @param commentId 评论唯一标识
     * @param userId 用户唯一标识
     */
    @Override
    public void toCommentLike(String commentId, String userId) {
        newsCommentsServiceFeign.toCommentLike(commentId, userId);
    }

    /**
     * 将评论模块返回的评论feign转化为前端需要的评论VO
     * @param commentsFeign 评论feign
     * @return 评论VO
     */
    private CommentsVO transformCommentFeign(CommentsFeign commentsFeign){
        CommentsVO commentsVO = new CommentsVO();
        commentsVO.setComment(commentsFeign.getComment());
        commentsVO.setCommentId(commentsFeign.getCommentId());
        commentsVO.setCommentLike(commentsFeign.getCommentLike());
        String userId = commentsFeign.getCreatedBy();
        ArrayList<String> nameAndHeadPic = queryUserNameAndHeadPic(userId);
        String userName = nameAndHeadPic.get(0);
        String headPic = nameAndHeadPic.get(1);
        commentsVO.setUserName(userName);
        commentsVO.setHeadPic(headPic);
        commentsVO.setNewsId(commentsFeign.getNewsId());
        commentsVO.setCreateTime(commentsFeign.getCreateTime());
        return commentsVO;
    }

    /**
     * 根据用户唯一标识获取用户的用户名和头像
     * @param userId 用户唯一标识
     * @return 包含用户名和头像的ArrayList
     */
    private ArrayList<String> queryUserNameAndHeadPic(String userId){
        User user = userServiceFeign.queryByUserId(userId);
        ArrayList<String> nameAndPic = new ArrayList<>();
        nameAndPic.add(user.getLoginName());
        nameAndPic.add(user.getHeadPicture());
        return nameAndPic;
    }

    /**
     * 将新闻服务模块返回的新闻feign转化为新闻VO
     * @param newsFeign 新闻服务返回的feign
     * @return 新闻VO
     */
    private NewsVO transformNewsFeign(NewsFeign newsFeign) {
        NewsVO newsVO = new NewsVO();
        newsVO.setNewsId(newsFeign.getNewsId());
        newsVO.setTitle(newsFeign.getTitle());
        newsVO.setContent(newsFeign.getContent());
        newsVO.setClick(newsFeign.getClick());
        newsVO.setCreateTime(newsFeign.getCreateTime());

        ArrayList<String> urls = new ArrayList<>();
        ArrayList<String> thumbnailUrls = new ArrayList<>();
        ArrayList<String> pictureIds = newsFeign.getPictureIds();
        for(String pictureId:pictureIds){
            String pictureUrl = pictureServiceFeign.getPictureUrl(pictureId);
            String thumbnailUrl = pictureServiceFeign.getThumbnailUrl(pictureId);
            urls.add(pictureUrl);
            thumbnailUrls.add(thumbnailUrl);
        }
        newsVO.setUrls(urls);
        newsVO.setThumbnailUrls(thumbnailUrls);

        return newsVO;
    }

}
